package FSMDE.persistence.tests;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.xml.parsers.ParserConfigurationException;

import junit.framework.Assert;

import main.java.ERMDE.models.Attribute;
import main.java.ERMDE.models.AttributeCollection;
import main.java.ERMDE.models.Entity;
import main.java.ERMDE.models.EntityCollection;
import main.java.ERMDE.models.EntityType;
import main.java.ERMDE.models.RelationshipCollection;
import main.java.FSMDE.models.Component;
import main.java.FSMDE.models.ComponentCollection;
import main.java.FSMDE.models.ComponentType;
import main.java.FSMDE.models.Diagram;
import main.java.FSMDE.models.FactoryComponent;
import main.java.FSMDE.models.FactoryTransition;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.InterfaceCollection;
import main.java.FSMDE.models.InterfaceGraphType;
import main.java.FSMDE.models.InterfaceType;
import main.java.FSMDE.models.RelatedComponent;
import main.java.FSMDE.models.Transition;
import main.java.FSMDE.models.TransitionCollection;
import main.java.FSMDE.models.TransitionControl;
import main.java.FSMDE.models.TransitionFunctional;
import main.java.FSMDE.models.TransitionType;
import main.java.FSMDE.persistence.DiagramXmlManager;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import ERMDE.persistence.tests.TestUtilities;

public class DiagramXmlManagerTestCase {

	private static final String fsmType = "fsm";

	@Test
	public void testShouldGenerateAXmlFromDiagram() throws Exception {
		InterfaceCollection infaceColl = new InterfaceCollection();
		TransitionCollection transColl = new TransitionCollection();
		List<Diagram> subDiagrams = new ArrayList<Diagram>();
		Diagram subDiagram1 = new Diagram();
		subDiagram1.setName("Sub1");
		Diagram subDiagram2 = new Diagram();
		subDiagram2.setName("Sub2");

		infaceColl.add(new Interface("inface1"));
		infaceColl.add(new Interface("inface2"));
		Transition trans = Transition.create("trans1", TransitionType.Control);
		trans.setDestination(infaceColl.get("inface1"));
		trans.setSource(infaceColl.get("inface2"));
		transColl.add(trans);
		Transition trans2 = Transition.create("trans2",
				TransitionType.Functional);
		trans2.setSource(infaceColl.get("inface2"));
		trans2.setDestination(infaceColl.get("inface1"));
		transColl.add(trans2);

		subDiagram1.setInterfaces(infaceColl);
		subDiagram2.setInterfaces(infaceColl);
		subDiagram1.addSubDiagram(subDiagram2);

		subDiagrams.add(subDiagram1);
		subDiagrams.add(subDiagram2);

		Diagram diagram = new Diagram();
		diagram.setInterfaces(infaceColl);
		diagram.setSubDiagrams(subDiagrams);
		diagram.setTransitions(transColl);
		diagram.setName("diag1");

		Document document = TestUtilities.createDocument();

		DiagramXmlManager xmlManager = new DiagramXmlManager();

		Element diagramElement = xmlManager.getElementFromItem(diagram,
				document);

		Assert.assertEquals("diagram", diagramElement.getTagName());
		Assert.assertEquals(diagram.getId().toString(),
				diagramElement.getAttribute("id"));

		Element infacesElement = (Element) diagramElement.getElementsByTagName(
				"interfaces").item(0);
		Element transitionsElement = (Element) diagramElement
				.getElementsByTagName("transitions").item(0);
		Element subDiagramsElement = (Element) diagramElement
				.getElementsByTagName("subdiagrams").item(0);

		NodeList items = infacesElement.getChildNodes();
		Assert.assertEquals(2, items.getLength());
		Assert.assertEquals("inface1",
				((Element) items.item(0)).getAttribute("name"));
		Assert.assertEquals("inface2",
				((Element) items.item(1)).getAttribute("name"));

		items = transitionsElement.getChildNodes();
		Assert.assertEquals(2, items.getLength());
		Assert.assertEquals(trans.getId().toString(),
				((Element) items.item(0)).getAttribute("id"));
		Assert.assertEquals(trans.getType().toString(),
				((Element) items.item(0)).getAttribute("type"));

		NodeList subDiagList = subDiagramsElement.getChildNodes();

		Assert.assertEquals(2, subDiagList.getLength());
		Assert.assertEquals(subDiagram1.getId().toString(),
				((Element) subDiagList.item(0)).getAttribute("id"));
		Assert.assertEquals(subDiagram1.getName(),
				((Element) subDiagList.item(0)).getAttribute("name"));
		Assert.assertEquals(1, ((Element) subDiagList.item(0))
				.getElementsByTagName("subdiagrams").getLength());
		Assert.assertEquals(
				infaceColl.get("inface2").getId().toString(),
				((Element) ((Element) ((Element) ((Element) subDiagList.item(0))
						.getElementsByTagName("subdiagrams").item(0))
						.getElementsByTagName("interfaces").item(0))
						.getChildNodes().item(1)).getAttribute("id").toString());
		Assert.assertEquals(subDiagram2.getId().toString(),
				((Element) subDiagList.item(1)).getAttribute("id"));
		Assert.assertEquals(subDiagram2.getName(),
				((Element) subDiagList.item(1)).getAttribute("name"));
	}

	@Test
	public void testShouldGenerateADiagramFromXml() throws Exception {
		String xml = "<diagram id='3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF' name='diag1' type='fsm'><interfaces>"
				+ "<interface name='inface1' id='3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF' type='General' graphType='Normal'/>"
				+ "<interface name='inface2' id='3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF' type='General' graphType='Normal'>"
				+
				// "<component name='comp1' type='button'/>" +
				"</interface></interfaces>"
				+ "<transitions><transition id='3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF' "
				+ "type='Control' name='trans1' source='3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF' destination='3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF' compTrigger=''/>"
				+ "</transitions><subdiagrams>"
				+ "<diagram id='3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF' name='sub1' type='fsm'/>"
				+ "<diagram id='3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF' name='sub2' type='fsm'/>"
				+ "</subdiagrams></diagram>";

		Document document = TestUtilities.loadXMLFromString(xml);

		DiagramXmlManager xmlManager = new DiagramXmlManager();

		Element diagramElement = document.getDocumentElement();

		Diagram diagram = xmlManager.getItemFromXmlElement(diagramElement);

		Assert.assertEquals("3f2504e0-4f89-11d3-9a0c-030dffffffff", diagram
				.getId().toString());
		Assert.assertEquals("diag1", diagram.getName());

		Assert.assertEquals(2, diagram.getInterfaces().count());
		Assert.assertNotNull(diagram.getInterface(UUID
				.fromString("3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF")));
		Assert.assertEquals("inface2", diagram.getInterfaces().get("inface2")
				.getName());
		Assert.assertEquals("3f2504e0-4f89-11d3-9a0c-030dffffffff", diagram
				.getInterfaces().get("inface1").getId().toString());
		Assert.assertEquals("General", diagram.getInterfaces().get("inface2")
				.getType().toString());

		Assert.assertEquals(1, diagram.getTransitions().count());
		Assert.assertNotNull(diagram.getTransition(UUID
				.fromString("3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF")));
		Assert.assertEquals(
				"trans1",
				diagram.getTransition(
						UUID.fromString("3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF"))
						.getName());
		Assert.assertEquals(
				"3f2504e0-4f89-11d3-9a0c-0300ffffffff",
				diagram.getTransition(
						UUID.fromString("3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF"))
						.getSource().getId().toString());
		Assert.assertEquals(
				"3f2504e0-4f89-11d3-9a0c-030dffffffff",
				diagram.getTransition(
						UUID.fromString("3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF"))
						.getDestination().getId().toString());

		Assert.assertEquals(2, diagram.getSubDiagrams().size());
		Assert.assertNotNull("Sub1", diagram.getSubDiagrams().get(0).getName());
		Assert.assertNotNull("Sub2", diagram.getSubDiagrams().get(1).getName());
		Assert.assertEquals("3f2504e0-4f89-11d3-9a0c-0300ffffffff", diagram
				.getSubDiagrams().get(0).getId().toString());

	}

	@Test
	public void testShouldCreateAnXmlFromASimpleDiagram() {

		// Simple diagram means that the diagram doesn't have subdiagram

		// arrange

		/*
		 * Creare el siguiente diagrama
		 * 
		 * Diagram->Interface0->componentTrigger0
		 * Diagram->Interface1->componentTrigger1 Diagram->Interface2
		 * Diagram->Transition0( s=interface0 d=interface1
		 * compTrigger=componentTrigger0 ) Diagram->Transition1( s=interface1
		 * d=interface2 compTrigger=componentTrigger1 ) Diagram->Transition2(
		 * s=interface0 d=interface2 function=functionName )
		 */
		Diagram diagram = new Diagram();
		diagram.setId(UUID.fromString("0-0-0-0-0"));
		diagram.setName("diagramName");

		Component componentTrigger0 = new Component(
				UUID.fromString("0-0-1-0-0"), "componentTrigger0Name",
				ComponentType.Button);
		ComponentCollection componentCollection0 = new ComponentCollection();
		componentCollection0.add(componentTrigger0);
		Component componentTrigger1 = new Component(
				UUID.fromString("0-0-1-0-1"), "componentTrigger1Name",
				ComponentType.Button);
		ComponentCollection componentCollection1 = new ComponentCollection();
		componentCollection1.add(componentTrigger1);

		Interface interface0 = new Interface(UUID.fromString("0-0-0-1-0"),
				"int0Name", InterfaceType.Entry_form, null,
				InterfaceGraphType.Starting_interface);
		interface0.setComponents(componentCollection0);
		Interface interface1 = new Interface(UUID.fromString("0-0-0-1-1"),
				"int1Name", InterfaceType.Output_form, null,
				InterfaceGraphType.Normal);
		interface1.setComponents(componentCollection1);
		Interface interface2 = new Interface(UUID.fromString("0-0-0-1-2"),
				"int2Name", InterfaceType.General, null,
				InterfaceGraphType.Ending_interface);
		InterfaceCollection interfaceCollection = new InterfaceCollection();
		interfaceCollection.add(interface0);
		interfaceCollection.add(interface1);
		interfaceCollection.add(interface2);
		diagram.setInterfaces(interfaceCollection);

		TransitionControl transition0 = (TransitionControl) Transition.create(
				"transition0Name", TransitionType.Control);
		transition0.setTriggerComponent(componentTrigger0);
		transition0.setSource(interface0);
		try {
			transition0.setDestination(interface1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransitionControl transition1 = (TransitionControl) Transition.create(
				"transition1Name", TransitionType.Control);
		transition1.setTriggerComponent(componentTrigger1);
		transition1.setSource(interface1);
		try {
			transition1.setDestination(interface2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransitionFunctional transition2 = (TransitionFunctional) Transition
				.create("transition2Name", TransitionType.Functional);
		transition2.setFunctionName("functionName");
		transition2.setSource(interface0);
		try {
			transition2.setDestination(interface2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransitionCollection transitionCollection = new TransitionCollection();
		transitionCollection.add(transition0);
		transitionCollection.add(transition1);
		transitionCollection.add(transition2);
		diagram.setTransitions(transitionCollection);

		// act
		Document document = null;
		try {
			document = TestUtilities.createDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		DiagramXmlManager xmlManager = new DiagramXmlManager();
		Element diagramElement = xmlManager.getElementFromItem(diagram,
				document);

		Element interfacesElement = (Element) diagramElement
				.getElementsByTagName("interfaces").item(0);
		Element transitionsElement = (Element) diagramElement
				.getElementsByTagName("transitions").item(0);
		Element subDiagramsElement = (Element) diagramElement
				.getElementsByTagName("subdiagrams").item(0);
		NodeList interfacesItems = interfacesElement.getChildNodes();
		NodeList transitionsItems = transitionsElement.getChildNodes();
		NodeList componentsInterface0List = ((Element) ((Element) interfacesItems
				.item(0)).getElementsByTagName("components").item(0))
				.getChildNodes();
		NodeList componentsInterface1List = ((Element) ((Element) interfacesItems
				.item(1)).getElementsByTagName("components").item(0))
				.getChildNodes();
		NodeList componentsInterface2List = ((Element) ((Element) interfacesItems
				.item(2)).getElementsByTagName("components").item(0))
				.getChildNodes();

		// assert diagram
		Assert.assertEquals("diagram", diagramElement.getTagName());
		Assert.assertEquals(diagram.getName(),
				diagramElement.getAttribute("name"));
		Assert.assertEquals(fsmType, diagramElement.getAttribute("type"));
		Assert.assertEquals(diagram.getId().toString(),
				diagramElement.getAttribute("id"));

		// assert interfaces
		Assert.assertEquals(3, interfacesItems.getLength());

		Assert.assertEquals(interface0.getId().toString(),
				((Element) interfacesItems.item(0)).getAttribute("id"));
		Assert.assertEquals(interface0.getName(),
				((Element) interfacesItems.item(0)).getAttribute("name"));
		Assert.assertEquals(interface0.getType().name(),
				((Element) interfacesItems.item(0)).getAttribute("type"));
		Assert.assertEquals(1, componentsInterface0List.getLength());
		Assert.assertEquals(componentTrigger0.getId().toString(),
				((Element) componentsInterface0List.item(0)).getAttribute("id"));
		Assert.assertEquals(componentTrigger0.getName(),
				((Element) componentsInterface0List.item(0))
						.getAttribute("name"));
		Assert.assertEquals(componentTrigger0.getType().name(),
				((Element) componentsInterface0List.item(0))
						.getAttribute("type"));

		Assert.assertEquals(interface1.getId().toString(),
				((Element) interfacesItems.item(1)).getAttribute("id"));
		Assert.assertEquals(interface1.getName(),
				((Element) interfacesItems.item(1)).getAttribute("name"));
		Assert.assertEquals(interface1.getType().name(),
				((Element) interfacesItems.item(1)).getAttribute("type"));
		Assert.assertEquals(1, componentsInterface1List.getLength());
		Assert.assertEquals(componentTrigger1.getId().toString(),
				((Element) componentsInterface1List.item(0)).getAttribute("id"));
		Assert.assertEquals(componentTrigger1.getName(),
				((Element) componentsInterface1List.item(0))
						.getAttribute("name"));
		Assert.assertEquals(componentTrigger1.getType().name(),
				((Element) componentsInterface1List.item(0))
						.getAttribute("type"));

		Assert.assertEquals(interface2.getId().toString(),
				((Element) interfacesItems.item(2)).getAttribute("id"));
		Assert.assertEquals(interface2.getName(),
				((Element) interfacesItems.item(2)).getAttribute("name"));
		Assert.assertEquals(interface2.getType().name(),
				((Element) interfacesItems.item(2)).getAttribute("type"));
		Assert.assertEquals(0, componentsInterface2List.getLength());

		// assert transitions
		Assert.assertEquals(3, transitionsItems.getLength());

		Assert.assertEquals(transition0.getId().toString(),
				((Element) transitionsItems.item(0)).getAttribute("id"));
		Assert.assertEquals(transition0.getName(),
				((Element) transitionsItems.item(0)).getAttribute("name"));
		Assert.assertEquals(transition0.getType().name(),
				((Element) transitionsItems.item(0)).getAttribute("type"));
		Assert.assertEquals(transition0.getSource().getId().toString(),
				((Element) transitionsItems.item(0)).getAttribute("source"));
		Assert.assertEquals(transition0.getDestination().getId().toString(),
				((Element) transitionsItems.item(0))
						.getAttribute("destination"));
		Assert.assertEquals(transition0.getTriggerComponent().getId()
				.toString(), ((Element) transitionsItems.item(0))
				.getAttribute("comptrigger"));
		Assert.assertEquals("",
				((Element) transitionsItems.item(0)).getAttribute("function"));

		Assert.assertEquals(transition1.getId().toString(),
				((Element) transitionsItems.item(1)).getAttribute("id"));
		Assert.assertEquals(transition1.getName(),
				((Element) transitionsItems.item(1)).getAttribute("name"));
		Assert.assertEquals(transition1.getType().name(),
				((Element) transitionsItems.item(1)).getAttribute("type"));
		Assert.assertEquals(transition1.getSource().getId().toString(),
				((Element) transitionsItems.item(1)).getAttribute("source"));
		Assert.assertEquals(transition1.getDestination().getId().toString(),
				((Element) transitionsItems.item(1))
						.getAttribute("destination"));
		Assert.assertEquals(transition1.getTriggerComponent().getId()
				.toString(), ((Element) transitionsItems.item(1))
				.getAttribute("comptrigger"));
		Assert.assertEquals("",
				((Element) transitionsItems.item(1)).getAttribute("function"));

		Assert.assertEquals(transition2.getId().toString(),
				((Element) transitionsItems.item(2)).getAttribute("id"));
		Assert.assertEquals(transition2.getName(),
				((Element) transitionsItems.item(2)).getAttribute("name"));
		Assert.assertEquals(transition2.getType().name(),
				((Element) transitionsItems.item(2)).getAttribute("type"));
		Assert.assertEquals(transition2.getSource().getId().toString(),
				((Element) transitionsItems.item(2)).getAttribute("source"));
		Assert.assertEquals(transition2.getDestination().getId().toString(),
				((Element) transitionsItems.item(2))
						.getAttribute("destination"));
		Assert.assertEquals("", ((Element) transitionsItems.item(2))
				.getAttribute("comptrigger"));
		Assert.assertEquals(transition2.getFunctionName(),
				((Element) transitionsItems.item(2)).getAttribute("function"));

		// assert sub diagrams
		Assert.assertNull(subDiagramsElement);
	}

	@Test
	public void testShouldCreateAnXmlFromAComplexDiagram() {
		// Complex diagram means that the diagram has subdiagrams

		// arrange

		/*
		 * Creare el siguiente diagrama
		 * 
		 * Diagram->Interface0->componentTrigger0
		 * Diagram->Interface1->componentTrigger1 Diagram->Interface2
		 * Diagram->Transition0( s=interface0 d=interface1
		 * compTrigger=componentTrigger0 ) Diagram->Transition1( s=interface1
		 * d=interface2 compTrigger=componentTrigger1 ) Diagram->Transition2(
		 * s=interface0 d=interface2 function=functionName0 )
		 * Diagram->SubDiagrams->Subdiagram0
		 * Diagram->SubDiagrams->Subdiagram1->Interface3
		 */

		Diagram diagram = new Diagram();
		diagram.setId(UUID.fromString("0-0-0-0-0"));
		diagram.setName("diagramName");

		Component componentTrigger0 = new Component(
				UUID.fromString("0-0-1-0-0"), "componentTrigger0Name",
				ComponentType.Button);
		ComponentCollection componentCollection0 = new ComponentCollection();
		componentCollection0.add(componentTrigger0);
		Component componentTrigger1 = new Component(
				UUID.fromString("0-0-1-0-1"), "componentTrigger1Name",
				ComponentType.Button);
		ComponentCollection componentCollection1 = new ComponentCollection();
		componentCollection1.add(componentTrigger1);

		Interface interface0 = new Interface(UUID.fromString("0-0-0-1-0"),
				"int0Name", InterfaceType.Entry_form, null,
				InterfaceGraphType.Starting_interface);
		interface0.setComponents(componentCollection0);
		Interface interface1 = new Interface(UUID.fromString("0-0-0-1-1"),
				"int1Name", InterfaceType.Output_form, null,
				InterfaceGraphType.Normal);
		interface1.setComponents(componentCollection1);
		Interface interface2 = new Interface(UUID.fromString("0-0-0-1-2"),
				"int2Name", InterfaceType.General, null,
				InterfaceGraphType.Ending_interface);
		InterfaceCollection interfaceCollection = new InterfaceCollection();
		interfaceCollection.add(interface0);
		interfaceCollection.add(interface1);
		interfaceCollection.add(interface2);
		diagram.setInterfaces(interfaceCollection);

		TransitionControl transition0 = (TransitionControl) Transition.create(
				"transition0Name", TransitionType.Control);
		transition0.setTriggerComponent(componentTrigger0);
		transition0.setSource(interface0);
		try {
			transition0.setDestination(interface1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransitionControl transition1 = (TransitionControl) Transition.create(
				"transition1Name", TransitionType.Control);
		transition1.setTriggerComponent(componentTrigger1);
		transition1.setSource(interface1);
		try {
			transition1.setDestination(interface2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransitionFunctional transition2 = (TransitionFunctional) Transition
				.create("transition2Name", TransitionType.Functional);
		transition2.setFunctionName("functionName");
		transition2.setSource(interface0);
		try {
			transition2.setDestination(interface2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransitionCollection transitionCollection = new TransitionCollection();
		transitionCollection.add(transition0);
		transitionCollection.add(transition1);
		transitionCollection.add(transition2);
		diagram.setTransitions(transitionCollection);

		// arrange subdiagrams

		ArrayList<Diagram> subDiagramList = new ArrayList<Diagram>();

		Diagram subdiagram0 = new Diagram();
		subdiagram0.setId(UUID.fromString("1-0-0-0-0"));
		subdiagram0.setName("subdiagram0Name");
		subDiagramList.add(subdiagram0);

		Diagram subdiagram1 = new Diagram();
		subdiagram1.setId(UUID.fromString("1-0-0-0-0"));
		subdiagram1.setName("subdiagram1Name");
		subDiagramList.add(subdiagram1);

		ComponentCollection componentCollection3 = new ComponentCollection();

		Interface interface3 = new Interface(UUID.fromString("0-0-0-3-0"),
				"int3Name", InterfaceType.Entry_form, null,
				InterfaceGraphType.Starting_interface);
		interface3.setComponents(componentCollection3);

		InterfaceCollection interfaceCollectionSubDiagram = new InterfaceCollection();
		interfaceCollectionSubDiagram.add(interface3);
		subdiagram1.setInterfaces(interfaceCollectionSubDiagram);

		diagram.setSubDiagrams(subDiagramList);

		// act
		Document document = null;
		try {
			document = TestUtilities.createDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		DiagramXmlManager xmlManager = new DiagramXmlManager();
		Element diagramElement = xmlManager.getElementFromItem(diagram,
				document);

		// validate

		Element interfacesElement = (Element) diagramElement
				.getElementsByTagName("interfaces").item(0);
		Element transitionsElement = (Element) diagramElement
				.getElementsByTagName("transitions").item(0);
		Element subDiagramsElement = (Element) diagramElement
				.getElementsByTagName("subdiagrams").item(0);
		NodeList interfacesItems = interfacesElement.getChildNodes();
		NodeList transitionsItems = transitionsElement.getChildNodes();
		NodeList componentsInterface0List = ((Element) ((Element) interfacesItems
				.item(0)).getElementsByTagName("components").item(0))
				.getChildNodes();
		NodeList componentsInterface1List = ((Element) ((Element) interfacesItems
				.item(1)).getElementsByTagName("components").item(0))
				.getChildNodes();
		NodeList componentsInterface2List = ((Element) ((Element) interfacesItems
				.item(2)).getElementsByTagName("components").item(0))
				.getChildNodes();
		NodeList subDiagList = subDiagramsElement.getChildNodes();

		// assert diagram
		Assert.assertEquals("diagramName", diagramElement.getAttribute("name"));
		Assert.assertEquals(diagram.getName(),
				diagramElement.getAttribute("name"));
		Assert.assertEquals(fsmType, diagramElement.getAttribute("type"));
		Assert.assertEquals(diagram.getId().toString(),
				diagramElement.getAttribute("id"));

		// assert interfaces
		Assert.assertEquals(3, interfacesItems.getLength());

		Assert.assertEquals(interface0.getId().toString(),
				((Element) interfacesItems.item(0)).getAttribute("id"));
		Assert.assertEquals(interface0.getName(),
				((Element) interfacesItems.item(0)).getAttribute("name"));
		Assert.assertEquals(interface0.getType().name(),
				((Element) interfacesItems.item(0)).getAttribute("type"));
		Assert.assertEquals(1, componentsInterface0List.getLength());
		Assert.assertEquals(componentTrigger0.getId().toString(),
				((Element) componentsInterface0List.item(0)).getAttribute("id"));
		Assert.assertEquals(componentTrigger0.getName(),
				((Element) componentsInterface0List.item(0))
						.getAttribute("name"));
		Assert.assertEquals(componentTrigger0.getType().name(),
				((Element) componentsInterface0List.item(0))
						.getAttribute("type"));

		Assert.assertEquals(interface1.getId().toString(),
				((Element) interfacesItems.item(1)).getAttribute("id"));
		Assert.assertEquals(interface1.getName(),
				((Element) interfacesItems.item(1)).getAttribute("name"));
		Assert.assertEquals(interface1.getType().name(),
				((Element) interfacesItems.item(1)).getAttribute("type"));
		Assert.assertEquals(1, componentsInterface1List.getLength());
		Assert.assertEquals(componentTrigger1.getId().toString(),
				((Element) componentsInterface1List.item(0)).getAttribute("id"));
		Assert.assertEquals(componentTrigger1.getName(),
				((Element) componentsInterface1List.item(0))
						.getAttribute("name"));
		Assert.assertEquals(componentTrigger1.getType().name(),
				((Element) componentsInterface1List.item(0))
						.getAttribute("type"));

		Assert.assertEquals(interface2.getId().toString(),
				((Element) interfacesItems.item(2)).getAttribute("id"));
		Assert.assertEquals(interface2.getName(),
				((Element) interfacesItems.item(2)).getAttribute("name"));
		Assert.assertEquals(interface2.getType().name(),
				((Element) interfacesItems.item(2)).getAttribute("type"));
		Assert.assertEquals(0, componentsInterface2List.getLength());

		// assert transitions
		Assert.assertEquals(3, transitionsItems.getLength());

		Assert.assertEquals(transition0.getId().toString(),
				((Element) transitionsItems.item(0)).getAttribute("id"));
		Assert.assertEquals(transition0.getName(),
				((Element) transitionsItems.item(0)).getAttribute("name"));
		Assert.assertEquals(transition0.getType().name(),
				((Element) transitionsItems.item(0)).getAttribute("type"));
		Assert.assertEquals(transition0.getSource().getId().toString(),
				((Element) transitionsItems.item(0)).getAttribute("source"));
		Assert.assertEquals(transition0.getDestination().getId().toString(),
				((Element) transitionsItems.item(0))
						.getAttribute("destination"));
		Assert.assertEquals(transition0.getTriggerComponent().getId()
				.toString(), ((Element) transitionsItems.item(0))
				.getAttribute("comptrigger"));
		Assert.assertEquals("",
				((Element) transitionsItems.item(0)).getAttribute("function"));

		Assert.assertEquals(transition1.getId().toString(),
				((Element) transitionsItems.item(1)).getAttribute("id"));
		Assert.assertEquals(transition1.getName(),
				((Element) transitionsItems.item(1)).getAttribute("name"));
		Assert.assertEquals(transition1.getType().name(),
				((Element) transitionsItems.item(1)).getAttribute("type"));
		Assert.assertEquals(transition1.getSource().getId().toString(),
				((Element) transitionsItems.item(1)).getAttribute("source"));
		Assert.assertEquals(transition1.getDestination().getId().toString(),
				((Element) transitionsItems.item(1))
						.getAttribute("destination"));
		Assert.assertEquals(transition1.getTriggerComponent().getId()
				.toString(), ((Element) transitionsItems.item(1))
				.getAttribute("comptrigger"));
		Assert.assertEquals("",
				((Element) transitionsItems.item(1)).getAttribute("function"));

		Assert.assertEquals(transition2.getId().toString(),
				((Element) transitionsItems.item(2)).getAttribute("id"));
		Assert.assertEquals(transition2.getName(),
				((Element) transitionsItems.item(2)).getAttribute("name"));
		Assert.assertEquals(transition2.getType().name(),
				((Element) transitionsItems.item(2)).getAttribute("type"));
		Assert.assertEquals(transition2.getSource().getId().toString(),
				((Element) transitionsItems.item(2)).getAttribute("source"));
		Assert.assertEquals(transition2.getDestination().getId().toString(),
				((Element) transitionsItems.item(2))
						.getAttribute("destination"));
		Assert.assertEquals("", ((Element) transitionsItems.item(2))
				.getAttribute("comptrigger"));
		Assert.assertEquals(transition2.getFunctionName(),
				((Element) transitionsItems.item(2)).getAttribute("function"));

		// assert sub diagrams
		Assert.assertEquals(2, subDiagList.getLength());

		// assert sub diagram 0
		Element subDiagram0Element = (Element) subDiagList.item(0);
		Element interfacesElementSub0 = (Element) subDiagram0Element
				.getElementsByTagName("interfaces").item(0);
		Element transitionsElementSub0 = (Element) subDiagram0Element
				.getElementsByTagName("transitions").item(0);
		Element subDiagramsElementSub0 = (Element) subDiagram0Element
				.getElementsByTagName("subdiagrams").item(0);

		Assert.assertEquals("subdiagram0Name",
				subDiagram0Element.getAttribute("name"));
		Assert.assertEquals(fsmType, subDiagram0Element.getAttribute("type"));
		Assert.assertEquals(subdiagram0.getId().toString(),
				subDiagram0Element.getAttribute("id"));
		Assert.assertEquals(null, interfacesElementSub0);
		Assert.assertEquals(null, transitionsElementSub0);
		Assert.assertEquals(null, subDiagramsElementSub0);

		// assert sub diagram 1
		Element subDiagram1Element = (Element) subDiagList.item(1);
		Element interfacesElementSub1 = (Element) subDiagram1Element
				.getElementsByTagName("interfaces").item(0);
		Element transitionsElementSub1 = (Element) subDiagram1Element
				.getElementsByTagName("transitions").item(0);
		Element subDiagramsElementSub1 = (Element) subDiagram1Element
				.getElementsByTagName("subdiagrams").item(0);

		Assert.assertEquals("subdiagram1Name",
				subDiagram1Element.getAttribute("name"));
		Assert.assertEquals(fsmType, subDiagram1Element.getAttribute("type"));
		Assert.assertEquals(subdiagram1.getId().toString(),
				subDiagram1Element.getAttribute("id"));
		Assert.assertEquals(1, interfacesElementSub1.getChildNodes()
				.getLength());
		Assert.assertEquals(null, transitionsElementSub1);
		Assert.assertEquals(null, subDiagramsElementSub1);
	}

	@Test
	public void testShouldSetTransitionToInterfaceTransitionCollection() {

		String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
				+ "<diagram id='29ed44d6-15fe-407c-8b07-1d1a25664b96' name='Principal' type='fsm'>"
				+ "<interfaces>"
				+ "<interface id='e9fd0ed9-40f8-4c72-8c89-1e27c63d5ac3' name='aa' type='General' graphType='Normal'>"
				+ "<components/>"
				+ "</interface>"
				+ "<interface id='280e898a-586d-4836-be43-7ecddaa4bd45' name='bb' type='General' graphType='Normal'>"
				+ "<components/>"
				+ "</interface>"
				+ "<interface id='4edbae23-2f79-4f0f-bbba-eae047e288d4' name='cc' type='General' graphType='Normal'>"
				+ "<components/>"
				+ "</interface>"
				+ "<interface id='c5180959-f401-4de9-9554-779b1cd9d661' name='dd' type='General' graphType='Normal'>"
				+ "<components/>"
				+ "</interface>"
				+ "</interfaces>"
				+ "<transitions>"
				+ "<transition comptrigger='' destination='280e898a-586d-4836-be43-7ecddaa4bd45' id='e7d5c7fd-0e3a-444e-9aaa-66e087459480' name='t1' source='e9fd0ed9-40f8-4c72-8c89-1e27c63d5ac3' type='Control'/>"
				+ "<transition comptrigger='' destination='4edbae23-2f79-4f0f-bbba-eae047e288d4' id='3089f044-a2c9-4b40-a808-adbcf62b1ef1' name='t2' source='280e898a-586d-4836-be43-7ecddaa4bd45' type='Control'/>"
				+ "<transition comptrigger='' destination='280e898a-586d-4836-be43-7ecddaa4bd45' id='ad381afa-8578-43d9-b7f9-50cb9c16c985' name='t3' source='4edbae23-2f79-4f0f-bbba-eae047e288d4' type='Control'/>"
				+ "<transition comptrigger='' destination='c5180959-f401-4de9-9554-779b1cd9d661' id='fbadea76-9e3e-45db-9e83-7728192a6eaa' name='t4' source='c5180959-f401-4de9-9554-779b1cd9d661' type='Control'/>"
				+ "</transitions>" + "</diagram>";

		// act

		Document document = null;
		try {
			document = TestUtilities.loadXMLFromString(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		DiagramXmlManager xmlManager = new DiagramXmlManager();
		Element diagramElement = document.getDocumentElement();
		Diagram diagram = null;
		try {
			diagram = xmlManager.getItemFromXmlElement(diagramElement);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// assert

		// interface data
		Interface interfaceAA = diagram.getInterface(UUID
				.fromString("e9fd0ed9-40f8-4c72-8c89-1e27c63d5ac3"));
		Interface interfaceBB = diagram.getInterface(UUID
				.fromString("280e898a-586d-4836-be43-7ecddaa4bd45"));
		Interface interfaceCC = diagram.getInterface(UUID
				.fromString("4edbae23-2f79-4f0f-bbba-eae047e288d4"));
		Interface interfaceDD = diagram.getInterface(UUID
				.fromString("c5180959-f401-4de9-9554-779b1cd9d661"));

		Assert.assertNotNull(interfaceAA);
		Assert.assertEquals("aa", interfaceAA.getName());
		Assert.assertEquals(InterfaceType.General, interfaceAA.getType());
		Assert.assertNotNull(interfaceBB);
		Assert.assertEquals("bb", interfaceBB.getName());
		Assert.assertEquals(InterfaceType.General, interfaceBB.getType());
		Assert.assertNotNull(interfaceCC);
		Assert.assertEquals("cc", interfaceCC.getName());
		Assert.assertEquals(InterfaceType.General, interfaceCC.getType());
		Assert.assertNotNull(interfaceDD);
		Assert.assertEquals("dd", interfaceDD.getName());
		Assert.assertEquals(InterfaceType.General, interfaceDD.getType());

		// transition data
		Transition transition1 = diagram.getTransition(UUID
				.fromString("e7d5c7fd-0e3a-444e-9aaa-66e087459480"));
		Transition transition2 = diagram.getTransition(UUID
				.fromString("3089f044-a2c9-4b40-a808-adbcf62b1ef1"));
		Transition transition3 = diagram.getTransition(UUID
				.fromString("ad381afa-8578-43d9-b7f9-50cb9c16c985"));
		Transition transition4 = diagram.getTransition(UUID
				.fromString("fbadea76-9e3e-45db-9e83-7728192a6eaa"));

		Assert.assertNotNull(transition1);
		Assert.assertEquals("t1", transition1.getName());
		Assert.assertEquals(TransitionType.Control, transition1.getType());
		Assert.assertNotNull(transition2);
		Assert.assertEquals("t2", transition2.getName());
		Assert.assertEquals(TransitionType.Control, transition1.getType());
		Assert.assertNotNull(transition3);
		Assert.assertEquals("t3", transition3.getName());
		Assert.assertEquals(TransitionType.Control, transition1.getType());
		Assert.assertNotNull(transition4);
		Assert.assertEquals("t4", transition4.getName());
		Assert.assertEquals(TransitionType.Control, transition1.getType());

		// sources and destinatios
		Assert.assertSame(interfaceAA, transition1.getSource());
		Assert.assertSame(interfaceBB, transition1.getDestination());

		Assert.assertSame(interfaceBB, transition2.getSource());
		Assert.assertSame(interfaceCC, transition2.getDestination());

		Assert.assertSame(interfaceCC, transition3.getSource());
		Assert.assertSame(interfaceBB, transition3.getDestination());

		Assert.assertSame(interfaceDD, transition4.getSource());
		Assert.assertSame(interfaceDD, transition4.getDestination());

		// related transitions
		Assert.assertEquals(1, interfaceAA.getTransitions().count());
		Assert.assertEquals(3, interfaceBB.getTransitions().count());
		Assert.assertEquals(2, interfaceCC.getTransitions().count());
		Assert.assertEquals(1, interfaceDD.getTransitions().count());

		Assert.assertTrue(interfaceAA.getTransitions().getTransitions()
				.contains(transition1));

		Assert.assertTrue(interfaceBB.getTransitions().getTransitions()
				.contains(transition1));
		Assert.assertTrue(interfaceBB.getTransitions().getTransitions()
				.contains(transition2));
		Assert.assertTrue(interfaceBB.getTransitions().getTransitions()
				.contains(transition3));

		Assert.assertTrue(interfaceCC.getTransitions().getTransitions()
				.contains(transition2));
		Assert.assertTrue(interfaceCC.getTransitions().getTransitions()
				.contains(transition3));

		Assert.assertTrue(interfaceDD.getTransitions().getTransitions()
				.contains(transition4));
	}

	@Test
	public void testShouldRetrieveDiagramWithComplexComponent() {

		// arrange

		String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
				+ "<diagram id='27332c28-0a41-4b2a-837c-a7a7c0087935' name='Principal' type='fsm'>"
				+ "<interfaces>"
				+ "<interface id='854b4412-efb3-4818-80ce-81aa26d075a4' name='aa' type='General' graphType='Normal'>"
				+ "<components>"
				+ "<component id='a1870665-a92f-4d0b-b88d-7265d087e7d4' name='hola' type='Text_field'/>"
				+ "</components>"
				+ "</interface>"
				+ "<interface id='b3fa3b7d-4567-4f7a-8207-ac2884192623' name='bb' type='General' graphType='Normal'>"
				+ "<components>"
				+ "<component id='ea398fd2-1b24-45bc-8f9d-d3069b592329' name='Formulario' type='Form'>"
				+ "<components>"
				+ "<component id='45ebe6ef-bd69-40d5-8220-1b4d596cec34' name='edad' type='Text_field'/>"
				+ "</components>"
				+ "<fsmErmRelations>"
				+ "</fsmErmRelations>"
				+ "</component>"
				+ "<component id='8dcbeae0-b126-452a-a238-bfefa38c4536' name='aceptar' type='Button'/>"
				+ "</components>"
				+ "</interface>"
				+ "<interface id='bfb9748d-a099-4861-a6bc-4b4150a189f8' name='cc' type='General' graphType='Normal'>"
				+ "<components/>"
				+ "</interface>"
				+ "</interfaces>"
				+ "<transitions>"
				+ "<transition comptrigger='' destination='b3fa3b7d-4567-4f7a-8207-ac2884192623' id='eaedd31f-785f-4c9f-8f83-1d94e751830e' name='t1' source='854b4412-efb3-4818-80ce-81aa26d075a4' type='Control'/>"
				+ "<transition comptrigger='' destination='bfb9748d-a099-4861-a6bc-4b4150a189f8' id='f9e484bf-6c42-4b78-9153-a147e9c26258' name='t2' source='b3fa3b7d-4567-4f7a-8207-ac2884192623' type='Control'/>"
				+ "</transitions>" + "</diagram>";

		// act

		Document document = null;
		try {
			document = TestUtilities.loadXMLFromString(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		DiagramXmlManager xmlManager = new DiagramXmlManager();
		Element diagramElement = document.getDocumentElement();
		Diagram diagram = null;
		try {
			diagram = xmlManager.getItemFromXmlElement(diagramElement);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// assert

		// interface data
		Interface interfaceAA = diagram.getInterface(UUID
				.fromString("854b4412-efb3-4818-80ce-81aa26d075a4"));
		Interface interfaceBB = diagram.getInterface(UUID
				.fromString("b3fa3b7d-4567-4f7a-8207-ac2884192623"));
		Interface interfaceCC = diagram.getInterface(UUID
				.fromString("bfb9748d-a099-4861-a6bc-4b4150a189f8"));

		Assert.assertNotNull(interfaceAA);
		Assert.assertEquals("aa", interfaceAA.getName());
		Assert.assertEquals(InterfaceType.General, interfaceAA.getType());
		Assert.assertNotNull(interfaceBB);
		Assert.assertEquals("bb", interfaceBB.getName());
		Assert.assertEquals(InterfaceType.General, interfaceBB.getType());
		Assert.assertNotNull(interfaceCC);
		Assert.assertEquals("cc", interfaceCC.getName());
		Assert.assertEquals(InterfaceType.General, interfaceCC.getType());

		// transition data
		Transition transition1 = diagram.getTransition(UUID
				.fromString("eaedd31f-785f-4c9f-8f83-1d94e751830e"));
		Transition transition2 = diagram.getTransition(UUID
				.fromString("f9e484bf-6c42-4b78-9153-a147e9c26258"));

		Assert.assertNotNull(transition1);
		Assert.assertEquals("t1", transition1.getName());
		Assert.assertEquals(TransitionType.Control, transition1.getType());
		Assert.assertNotNull(transition2);
		Assert.assertEquals("t2", transition2.getName());
		Assert.assertEquals(TransitionType.Control, transition1.getType());

		// sources and destinatios
		Assert.assertSame(interfaceAA, transition1.getSource());
		Assert.assertSame(interfaceBB, transition1.getDestination());

		Assert.assertSame(interfaceBB, transition2.getSource());
		Assert.assertSame(interfaceCC, transition2.getDestination());

		// related transitions
		Assert.assertEquals(1, interfaceAA.getTransitions().count());
		Assert.assertEquals(2, interfaceBB.getTransitions().count());
		Assert.assertEquals(1, interfaceCC.getTransitions().count());

		Assert.assertTrue(interfaceAA.getTransitions().getTransitions()
				.contains(transition1));

		Assert.assertTrue(interfaceBB.getTransitions().getTransitions()
				.contains(transition1));
		Assert.assertTrue(interfaceBB.getTransitions().getTransitions()
				.contains(transition2));

		Assert.assertTrue(interfaceCC.getTransitions().getTransitions()
				.contains(transition2));

		// components
		Assert.assertEquals(1, interfaceAA.getComponents().count());
		Assert.assertEquals(2, interfaceBB.getComponents().count());
		Assert.assertEquals(0, interfaceCC.getComponents().count());

	}

	@Test
	public void testShouldGenerateAnXmlFromARelatedDiagramWithAnErm() {

		// arrange

		EntityCollection entities = new EntityCollection();
		AttributeCollection attributes01 = new AttributeCollection();
		Attribute attribute01 = new Attribute("attribute01");
		attribute01.setId(UUID.fromString("2-0-0-1-0"));
		try {
			attributes01.addAttribute(attribute01);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Entity entity01 = new Entity("entity01", UUID.fromString("2-0-1-0-0"),
				EntityType.None, attributes01);
		entities.add(entity01);
		AttributeCollection attributes02 = new AttributeCollection();
		Attribute attribute02 = new Attribute("attribute02");
		attribute02.setId(UUID.fromString("2-0-0-2-0"));
		try {
			attributes02.addAttribute(attribute02);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Entity entity02 = new Entity("entity02", UUID.fromString("2-0-2-0-0"),
				EntityType.None, attributes02);
		entities.add(entity02);

		RelationshipCollection relationships = new RelationshipCollection();

		main.java.ERMDE.models.Diagram ermDiagram = new main.java.ERMDE.models.Diagram();
		String diagramName = "Principal", projectName = "ERMProject";
		/*
		 * When we get from ProjectController all Diagrams for being setted to
		 * FSM.DiagramController we append the projectName to the beggining of
		 * the ErmDiagram's names separated by the character '|'
		 */
		ermDiagram.setName(projectName + "|" + diagramName);
		ermDiagram.setEntities(entities);
		ermDiagram.setRelationships(relationships);

		InterfaceCollection interfaces = new InterfaceCollection();
		Interface interface01 = new Interface("int01");
		interface01.setId(UUID.fromString("1-0-1-0-0"));
		Component button = FactoryComponent.create("button",
				ComponentType.Button);
		button.setId(UUID.fromString("1-0-0-0-1"));
		interface01.getComponents().add(button);
		interfaces.add(interface01);
		Interface interface02 = new Interface("int02");
		interface02.setId(UUID.fromString("1-0-2-0-0"));
		RelatedComponent form = (RelatedComponent) FactoryComponent.create(
				"form", ComponentType.Form);
		form.setId(UUID.fromString("1-0-0-0-2"));
		/*
		 * Component textField = FactoryComponent.create("text_field",
		 * ComponentType.Text_field);
		 * textField.setId(UUID.fromString("1-0-0-3-0"));
		 * form.getComponents().add(textField);
		 */
		interface02.getComponents().add(form);
		interfaces.add(interface02);

		TransitionCollection transitions = new TransitionCollection();
		TransitionFunctional transition = FactoryTransition
				.createFunctionalTransition("transition");
		transition.setId(UUID.fromString("1-0-0-1-0"));
		transition.setFunctionName("functionName");
		transition.setSource(interface01);
		transition.setDestination(interface02);
		interface01.getTransitions().add(transition);
		interface02.getTransitions().add(transition);
		transitions.add(transition);

		Diagram fsmDiagram = new Diagram();
		fsmDiagram.setId(UUID.fromString("1-1-0-0-0"));
		fsmDiagram.setName("Principal");
		fsmDiagram.setInterfaces(interfaces);
		fsmDiagram.setTransitions(transitions);

		// arranging the relation between the diagrams

		try {
			form.setRelationToErmAttribute(ermDiagram, entity01, entity01
					.getAttributes().getAttribute("attribute01"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			form.setRelationToErmEntity(ermDiagram, entity02);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		// act
		Document document = null;
		try {
			document = TestUtilities.createDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		DiagramXmlManager xmlManager = new DiagramXmlManager();
		Element diagramElement = xmlManager.getElementFromItem(fsmDiagram,
				document);

		// validate

		Element interfacesElement = (Element) diagramElement
				.getElementsByTagName("interfaces").item(0);
		NodeList interfacesItems = interfacesElement.getChildNodes();
		NodeList interface02NodeList = (((Element) interfacesItems)
				.getElementsByTagName("interface").item(1)).getChildNodes();

		NodeList componentsInterface02List = ((Element) interface02NodeList)
				.getElementsByTagName("components").item(0).getChildNodes();

		Assert.assertEquals(1, componentsInterface02List.getLength());
		Assert.assertEquals("form", ((Element) componentsInterface02List
				.item(0)).getAttribute("name"));
		Assert.assertEquals(ComponentType.Form.toString(),
				((Element) componentsInterface02List.item(0))
						.getAttribute("type"));

		NodeList componentsFormList = ((Element) ((Element) componentsInterface02List
				.item(0)).getElementsByTagName("components").item(0))
				.getChildNodes();

		// validate the creation of the text fields
		Assert.assertEquals(2, componentsFormList.getLength());
		Assert.assertEquals("attribute01",
				((Element) componentsFormList.item(0)).getAttribute("name"));
		Assert.assertEquals("Text_field",
				((Element) componentsFormList.item(0)).getAttribute("type"));
		Assert.assertEquals("attribute02",
				((Element) componentsFormList.item(1)).getAttribute("name"));
		Assert.assertEquals("Text_field",
				((Element) componentsFormList.item(1)).getAttribute("type"));

		/*
		 * validate the creation of the <fsmErmRelations> <relation
		 * componentId="" ermDiagramName="" entityId="" attributeId="" />
		 * <relation /> </fsmErmRelations>
		 */

		NodeList relationList = ((Element) interface02NodeList)
				.getElementsByTagName("fsmErmRelations").item(0)
				.getChildNodes();
		NodeList relation01 = ((Element) relationList)
				.getElementsByTagName("relation").item(0).getChildNodes();
		NodeList relation02 = ((Element) relationList)
				.getElementsByTagName("relation").item(1).getChildNodes();

		Assert.assertEquals(form.getComponents().get(attribute01.getName())
				.getId().toString(),
				((Element) relation01).getAttribute("componentId"));
		Assert.assertEquals(ermDiagram.getId().toString(),
				((Element) relation01).getAttribute("ermDiagramId"));
		Assert.assertEquals(entity01.getId().toString(),
				((Element) relation01).getAttribute("entityId"));
		Assert.assertEquals(attribute01.getId().toString(),
				((Element) relation01).getAttribute("attributeId"));

		Assert.assertEquals(form.getComponents().get(attribute02.getName())
				.getId().toString(),
				((Element) relation02).getAttribute("componentId"));
		Assert.assertEquals(ermDiagram.getId().toString(),
				((Element) relation02).getAttribute("ermDiagramId"));
		Assert.assertEquals(entity02.getId().toString(),
				((Element) relation02).getAttribute("entityId"));
		Assert.assertEquals(attribute02.getId().toString(),
				((Element) relation02).getAttribute("attributeId"));
	}

	@Test
	public void testShouldRetrieveADiagramRelatedFromXml() {

		// arrange

		EntityCollection entities = new EntityCollection();
		AttributeCollection attributes01 = new AttributeCollection();

		Attribute attribute01 = new Attribute("clave");
		attribute01.setId(UUID
				.fromString("937abc56-9f3e-47c7-b523-12359baee557"));
		try {
			attributes01.addAttribute(attribute01);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		Attribute attribute02 = new Attribute("firma");
		attribute02.setId(UUID
				.fromString("1bdca352-9925-4f2a-9afe-33ff50ae5cad"));
		try {
			attributes01.addAttribute(attribute02);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		Entity entity01 = new Entity("entity01",
				UUID.fromString("b71cb11b-afb8-419b-a26f-726d13103087"),
				EntityType.None, attributes01);
		entities.add(entity01);

		RelationshipCollection relationships = new RelationshipCollection();

		main.java.ERMDE.models.Diagram ermDiagram = new main.java.ERMDE.models.Diagram(
				UUID.fromString("5658ec82-efc3-4674-8577-c7dcd510a18f"));
		String diagramName = "Principal", projectName = "ERMProject";
		/*
		 * When we get from ProjectController all Diagrams for being setted to
		 * FSM.DiagramController we append the projectName to the beggining of
		 * the ErmDiagram's names separated by the character '|'
		 */
		ermDiagram.setName(projectName + "|" + diagramName);
		ermDiagram.setEntities(entities);
		ermDiagram.setRelationships(relationships);

		ArrayList<main.java.ERMDE.models.Diagram> ermDiagrams = new ArrayList<main.java.ERMDE.models.Diagram>();
		ermDiagrams.add(ermDiagram);

		String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
				+ "<diagram id='cf1573cf-ed4b-41ba-85d4-1bd3cc8bd9b1' name='Principal' type='fsm'>"
				+ "<interfaces>"
				+ "<interface id='1048e43e-9d01-40cc-bb4f-6744aa1d8f5c' name='asasasa' type='General' graphType='Normal'>"
				+ "<components>"
				+ "<component id='423020af-83f6-465a-874f-69d10d67cadc' name='formulario' type='Form'>"
				+ "<components>"
				+ "<component id='658a1033-8efb-4e0b-9ba1-6d6ce3fc09d5' name='aceptar' type='Button'/>"
				+ "<component id='adcc8a99-1a48-4cae-9618-fa447e3a2091' name='clave' type='Text_field'/>"
				+ "<component id='2a149501-801e-44bb-8ee3-4a685750508b' name='firma' type='Text_field'/>"
				+ "</components>"
				+ "<fsmErmRelations>"
				+ "<relation attributeId='937abc56-9f3e-47c7-b523-12359baee557' componentId='adcc8a99-1a48-4cae-9618-fa447e3a2091' entityId='b71cb11b-afb8-419b-a26f-726d13103087' ermDiagramId='5658ec82-efc3-4674-8577-c7dcd510a18f'/>"
				+ "<relation attributeId='1bdca352-9925-4f2a-9afe-33ff50ae5cad' componentId='2a149501-801e-44bb-8ee3-4a685750508b' entityId='b71cb11b-afb8-419b-a26f-726d13103087' ermDiagramId='5658ec82-efc3-4674-8577-c7dcd510a18f'/>"
				+ "</fsmErmRelations>" + "</component>" + "</components>"
				+ "</interface>" + "</interfaces>" + "</diagram>";

		// act

		Document document = null;
		try {
			document = TestUtilities.loadXMLFromString(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		DiagramXmlManager xmlManager = new DiagramXmlManager();
		Element diagramElement = document.getDocumentElement();
		Diagram diagram = null;
		try {
			xmlManager.setErmDiagramas(ermDiagrams);
			diagram = xmlManager.getItemFromXmlElement(diagramElement);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// assert
		Assert.assertEquals(1, diagram.getInterfaces().count());
		Interface interface0 = diagram.getInterface(UUID
				.fromString("1048e43e-9d01-40cc-bb4f-6744aa1d8f5c"));
		Assert.assertNotNull(interface0);

		Assert.assertEquals(1, interface0.getComponents().count());
		Component component = interface0.getComponents().get(
				UUID.fromString("423020af-83f6-465a-874f-69d10d67cadc"));
		Assert.assertNotNull(component);

		RelatedComponent form = (RelatedComponent) component;
		Assert.assertEquals(3, form.getComponents().count());
		Component button = form.getComponents().get(
				UUID.fromString("658a1033-8efb-4e0b-9ba1-6d6ce3fc09d5"));
		Assert.assertNotNull(button);
		Component relatedTextField01 = form.getComponents().get(
				UUID.fromString("adcc8a99-1a48-4cae-9618-fa447e3a2091"));
		Assert.assertNotNull(relatedTextField01);
		Component relatedTextField02 = form.getComponents().get(
				UUID.fromString("2a149501-801e-44bb-8ee3-4a685750508b"));
		Assert.assertNotNull(relatedTextField02);

		Assert.assertNotNull(form.getRelatedErmModel(relatedTextField01));
		Assert.assertNotNull(form.getRelatedErmModel(relatedTextField02));

		Assert.assertEquals(relatedTextField01.getName() + " : "
				+ relatedTextField01.getType() + " [" + ermDiagram.getName()
				+ " " + entity01.getName() + " " + attribute01.getName() + "]",
				relatedTextField01.toString());
	}
}
